<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <a-row :gutter="24">
        <a-col :span="24 - gutter" v-if="deptFlag">
          <!-- 部门树 -->
          <dept-tree ref="deptTree" :deptOptions="deptOptions" @select="clickDeptNode" />
        </a-col>
        <a-col :span="gutter">
          <!-- 条件搜索 -->
          <div class="table-page-search-wrapper">
            <a-form layout="inline">
              <a-row :gutter="48">
                <a-col :md="8" :sm="24">
                  <a-form-item label="月份" prop="date">
                    <a-month-picker v-model="queryParam.date" allow-clear placeholder="请选择上课日期" @change="changeTime" />
                  </a-form-item>
                </a-col>
                <a-col :md="!advanced && 8 || 24" :sm="24">
                  <span class="table-page-search-submitButtons"
                    :style="advanced && { float: 'right', overflow: 'hidden' } || {}">
                    <a-button type="primary" @click="handleQuery"><a-icon type="search" />查询</a-button>&nbsp;&nbsp;&nbsp;
                    <a-button type="primary" @click="rankQuery"><a-icon type="search" v-hasPermi="['student:attence']" />出勤排名</a-button>&nbsp;&nbsp;&nbsp;
                    <a-button type="primary" @click="handleExport" v-hasPermi="['manager:student:attenceExport']"> <a-icon type="download" />导出 </a-button>
                  </span>
                </a-col>
              </a-row>
            </a-form>
          </div>

          <!-- 数据展示 -->
          <a-list :grid="{ gutter: 16, xl: 4, lg: 3, md: 3, sm: 2, xs: 1 }" style="margin: 0 -8px">
            <a-list-item :key="item" v-for="item in list" style="padding: 0 8px">
              <a-card>
                <a-card-meta :title="item.type">
                  <a-avatar slot="avatar" style="color: white; backgroundColor: #1890ff">{{ item.type }}</a-avatar>
                </a-card-meta>
                <div class="content">
                  <div>
                    <p>出勤记录</p>
                    <p>{{ item.value }}{{ item.enable == 1 ? '天' : '次' }}</p>
                  </div>
                </div>
              </a-card>
            </a-list-item>
          </a-list>
        </a-col>
      </a-row>
      <a-modal v-model:visible="visible" :width="800" title="出勤排名" @ok="visible = false">
        <a-tabs default-active-key="1" size="large" :tab-bar-style="{ marginBottom: '24px', paddingLeft: '16px' }">
          <a-tab-pane key="1" tab="时间卡">
            <a-collapse v-model:activeKey="activeKey">
              <template v-for="item in rank.time">
                <a-collapse-panel key="1" :header="item.title">
                  <template v-for="data in item.list">
                    {{ data.name }}
                  </template>
                </a-collapse-panel>
              </template>
            </a-collapse>
          </a-tab-pane>
          <a-tab-pane key="2" tab="次卡">
            <a-collapse v-model:activeKey="activeKey">
              <template v-for="item in rank.count">
                <a-collapse-panel key="1" :header="item.title">
                  <template v-for="data in item.list">
                    {{ data.name }}
                  </template>
                </a-collapse-panel>
              </template>
            </a-collapse>
          </a-tab-pane>
        </a-tabs>
      </a-modal>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import storage from 'store'
import { getScheudleStatis, getAttenceRank } from '@/api/manager/perform'
import { tableMixin } from '@/store/table-mixin'
import { treeselect } from '@/api/system/dept'
import DeptTree from '../../system/user/modules/DeptTree'
import moment from 'moment'

export default {
  name: 'Attence',
  mixins: [tableMixin],
  components: {
    DeptTree,
  },
  data() {
    return {
      rank: {},
      activeKey: 1,
      visible: false,
      deptFlag: false,
      gutter: 24,
      deptId: null,
      rankList: [],
      visible: false,
      list: [],
      selectedRowKeys: [],
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      ids: [],
      loading: false,
      total: 0,
      // 查询参数
      queryParam: {
      },
      deptOptions: [],
    }
  },
  filters: {
  },
  created() {
    this.queryParam.date = moment().format('YYYY-MM')
    this.deptInit();
    this.getList()
    this.getTreeselect()
    this.deptId = storage.get("deptId");
  },
  computed: {
  },
  watch: {
  },
  methods: {
    /** 导出按钮操作 */
    handleExport () {
      var that = this
      this.$confirm({
        title: '是否确认导出?',
        onOk () {
          that.download('manager/student/attenceExport', {
            ...that.queryParam
          }, `student_${new Date().getTime()}.xlsx`)
        },
        onCancel () {}
      })
    },
    changeTime(val) {
      this.queryParam.date = val.format('YYYY-MM')
    },
    deptInit() {
      this.deptFlag = storage.get("deptId") == 100;
      this.queryParam.deptId = storage.get("deptId");
      if (this.queryParam.deptId == null) {
        this.queryParam.deptId = storage.get("deptId")
      }

      if (this.deptFlag) {
        this.gutter = 20;
      } else {
        this.gutter = 24;
      }
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data
      })
    },
    clickDeptNode(deptId) {
      this.queryParam.deptId = deptId
      this.handleQuery()
    },
    /** 查询校区业绩列表 */
    getList() {
      this.loading = true
      if (this.queryParam.deptId == null || this.queryParam.deptId < 100) {
        this.queryParam.deptId = storage.get("deptId")
      }
      getScheudleStatis(this.queryParam).then(response => {
        this.list = response.data
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParam.pageNum = 1
      this.getList()
    },
    rankQuery() {
      this.visible = true
      getAttenceRank(this.queryParam).then(resp => {
        this.rank = resp.data
      })
    },
  }
}
</script>
<style lang="less" scoped>
.clearfix() {
  zoom: 1;

  &:before,
  &:after {
    content: ' ';
    display: table;
  }

  &:after {
    clear: both;
    visibility: hidden;
    font-size: 0;
    height: 0;
  }
}

.content {
  .clearfix();
  margin-top: 16px;
  margin-left: 40px;

  &>div {
    position: relative;
    text-align: left;
    float: left;
    width: 50%;

    p {
      line-height: 32px;
      font-size: 24px;
      margin: 0;
    }

    p:first-child {
      color: 'rgba(0, 0, 0, 0.45)';
      font-size: 12px;
      line-height: 20px;
      margin-bottom: 4px;
    }
  }
}
</style>